package yt.kpool.activity.personal;

import android.content.Intent;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.bean.BaseBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TimeCountUtil;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.dialog.SuccessDialog;

/**
 * 注册
 */
public class RegisterActivity extends AppCompatActivity {
    @BindView(R.id.tv_verificationcode)
    TextView tv_verificationcode;
    @BindView(R.id.edit_phone)
    TextInputEditText edit_phone;
    @BindView(R.id.edit_loginpwd)
    TextInputEditText edit_loginpwd;
    @BindView(R.id.edit_surepwd)
    TextInputEditText edit_surepwd;
    @BindView(R.id.edit_name)
    TextInputEditText edit_name;
    @BindView(R.id.tv_vercode)
    TextInputEditText edit_vercode;  //验证码

    @BindView(R.id.edit_InvitationCode)
    TextInputEditText edit_InvitationCode;//邀请码
    private TimeCountUtil timeCountUtil;//时间计时

    private String tel="";
    private LoadingDialog loadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        timeCountUtil = new TimeCountUtil(RegisterActivity.this, 60000, 1000, tv_verificationcode,getResources().getColor(R.color.system_color));
        loadingDialog=new LoadingDialog(this,0);
    }

    @OnClick({R.id.tv_verificationcode, R.id.btn_register, R.id.title_left, R.id.title_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_verificationcode:
                //获取验证码
                if (edit_phone.getText().toString().trim().length() == 11) {
                   tel= edit_phone.getText().toString().trim();
                   sendSMS();
                } else {
                    ToastUtil.showToast(RegisterActivity.this, "请填写正确的手机号码");
                }

                break;
            case R.id.title_left://返回键
                finish();
                break;
            case R.id.title_right:
                //登录
                finish();
                break;
            case R.id.btn_register:
                if (check())
                    register();
                    break;

        }
    }

    private void register() {
        if (NetWorkUtil.check(this)) {

            loadingDialog.show();
            NetHelperNew.Register(tel,edit_InvitationCode.getText().toString().trim(),edit_name.getText().toString().trim(),edit_loginpwd.getText().toString().trim(),edit_vercode.getText().toString().trim(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    Log.i("-->", "注册:" + data);
                    if (baseBean.getType() == 1) {

                        new SuccessDialog(RegisterActivity.this, "注册成功", new SuccessDialog.OnSuccessListener() {
                            @Override
                            public void getResult() {
                                finish();
                            }
                        }).show();
                    } else {

                        new ErrorDialog(RegisterActivity.this,baseBean.getContent()).show();
//                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    new ErrorDialog(RegisterActivity.this,msg).show();
//                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }
    private boolean isSms=true;
    /**
     * 发送验证码
     */
    private void sendSMS() {
        if (isSms&&NetWorkUtil.check(this)) {

            isSms=false;
            NetHelperNew.RegSendSmsCode(tel, new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    isSms=true;

                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    Log.i("-->", "注册发送验证码:" + baseBean.getContent());
                    if (baseBean.getType() == 1) {
                        //倒计时
                        timeCountUtil.start();
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());

                    } else {

                        new ErrorDialog(RegisterActivity.this,baseBean.getContent()).show();
//                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    isSms=true;
                    new ErrorDialog(RegisterActivity.this,msg).show();
//                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }
    private boolean check() {
        if (edit_phone.getText().toString().trim().equals("")) {
            ToastUtil.showToast(this, "请输入手机号");
            return false;
        }

        if (edit_name.getText().toString().trim().equals("")) {
            ToastUtil.showToast(this, "请输入姓名");
            return false;
        }
        if (edit_loginpwd.getText().toString().trim().equals("")) {
            ToastUtil.showToast(this, "请输入密码");
            return false;
        }
        if (edit_surepwd.getText().toString().trim().equals("")) {
            ToastUtil.showToast(this, "请输入确认密码");
            return false;
        }

        if (edit_vercode.getText().toString().trim().equals("")) {
            ToastUtil.showToast(this, "请输入短信验证码");
            return false;
        }
        if (!edit_loginpwd.getText().toString().trim().equals(edit_surepwd.getText().toString().trim())) {
            ToastUtil.showToast(this, "两次密码不一致，请重新输入");
            return false;
        }

        return true;
    }

}
